// const baseUrl = 'http://112.126.84.165:8080/'
const baseUrl = 'https://www.krtwl.com/'
let getHeader = {
	'X-Requested-With': 'XMLHttpRequest',
	"Accept": "application/json",
	"Content-Type": "application/json;charset=UTF-8"
}

export const http = (opts) => {
	let postHeader = {
		'X-Requested-With': 'XMLHttpRequest',
		"Accept": "application/json",
		"Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
	}
	let params = {
		url: baseUrl + opts.url,
		data: opts.params,
		method: opts.method,
		header: opts.method.toLowerCase() == 'get' ? getHeader : postHeader,
		dataType: 'json'
	}
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中···',
			mask: true
		})
		uni.request(params).then(res => {
			uni.hideLoading();
			//请求成功201
			if (200 <= res[1].statusCode && res[1].statusCode <= 300) {
				
				if(res[1].data.errcode) {
					uni.showToast({
						title: res[1].data.errmsg,
						icon: 'none',
						mask: true
					})
				} else {
					resolve(res[1].data);
				}
				
			} else {
				uni.showToast({
					title: res[1].data.errmsg || '服务异常，请联系客服',
					icon: 'none',
					mask: true
				})
			}
		}).catch(e => {
			uni.hideLoading();
			uni.showToast({
				title: '请求错误',
				icon: 'none',
				mask: true
			})
			console.log(e);
			reject(e);
		})
	})
}
export const httpNoToast = (opts) => {
	let postHeader = {
		'X-Requested-With': 'XMLHttpRequest',
		"Accept": "application/json",
		"Content-Type": "application/json;charset=UTF-8"
	}
	let params = {
		url: baseUrl + opts.url,
		data: opts.params,
		method: opts.method,
		header: opts.method.toLowerCase() == 'get' ? getHeader : postHeader,
		dataType: 'json'
	}
	return new Promise((resolve, reject) => {
		uni.request(params).then(res => {
			//请求成功201
			if (200 <= res[1].statusCode && res[1].statusCode <= 300) {
				if(res[1].data.errcode) {
					uni.showToast({
						title: res[1].data.errmsg,
						icon: 'none',
						mask: true
					})
				} else {
					resolve(res[1].data);
				}
			}else {
				uni.showToast({
					title: res[1].data.errmsg || '服务异常，请联系客服',
					icon: 'none',
					mask: true
				})
			}
		}).catch(e => {
			uni.hideLoading();
			uni.showToast({
				title: '请求错误',
				icon: 'none',
				mask: true
			})
			console.log(e);
			reject(e);
		})
	})
}

export const httpToken = (opts) => {
	// 添加token
	let postHeader = {
		'Authorization': '',
		'X-Requested-With': 'XMLHttpRequest',
		'Content-Type': 'application/json;charset=UTF-8'
	}
	// getHeader['token'] = uni.getStorageSync('userInfo').token
	// postHeader['token'] = uni.getStorageSync('userInfo').token
	let params = {
		url: baseUrl + opts.url,
		data: opts.params,
		method: opts.method,
		header: opts.method.toLowerCase() == 'get' ? getHeader : postHeader,
		dataType: 'json'
	}

	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中···',
			mask: true
		})
		uni.request(params).then(res => {
			uni.hideLoading();
			if (200 <= res[1].statusCode && res[1].statusCode <= 300) {
				if (res[1].data.code === 2002) {
					uni.$emit('refreshTokens')
				} 
				resolve(res[1].data);
			} else{
				uni.showToast({
					title: res[1].data.errmsg || '服务异常，请联系客服',
					icon: 'none',
					mask: true
				})
			}
		}).catch(e => {
			uni.hideLoading();
			uni.showToast({
				title: '请求错误',
				icon: 'none'
			})
			console.log(e);
			reject(e);
		})
	})
}
//没有提示框  携带token
export const httpNoToastToken = (opts) => {
	const userInfo = uni.getStorageSync('userInfo');
	let postHeader = {
		'Authorization': '',
		'X-Requested-With': 'XMLHttpRequest',
		'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
	}
	// getHeader['token'] = uni.getStorageSync('userInfo').token
	// postHeader['token'] = uni.getStorageSync('userInfo').token
	let params = {
		url: baseUrl + opts.url,
		data: opts.params,
		method: opts.method,
		header: opts.method.toLowerCase() == 'get' ? getHeader : postHeader,
		dataType: 'json'
	}

	return new Promise((resolve, reject) => {
		uni.request(params).then(res => {
			uni.hideLoading();
			if (200 <= res[1].statusCode && res[1].statusCode <= 300) {
				console.log(res[1].data.code)
				if (res[1].data.code === 2002 || res[1].data.code === 2005) {
					uni.$emit('refreshTokens')
				} 
				resolve(res[1].data);
			} else{
				console.log(res[1].data)
				uni.showToast({
					title: res[1].data.errmsg || '服务异常，请联系客服',
					icon: 'none',
					mask: true
				})
			}
		}).catch(e => {
			uni.hideLoading();
			uni.showToast({
				title: '请求错误',
				icon: 'none'
			})
			console.log(e);
			reject(e);
		})
	})
}

export default {
	http,
	httpToken,
	httpNoToast,
	httpNoToastToken
}
